//
//  LayerAffineTransformViewController.m
//  CoreAnimation
//
//  Created by 张国文 on 16/10/10.
//  Copyright © 2016年 hangzhouyijiyin. All rights reserved.
//

#import "LayerAffineTransformViewController.h"

@interface LayerAffineTransformViewController ()

@property (nonatomic, strong) UIImageView *peopleImage;
@property (nonatomic, strong) UIView *showCommenPointView;
@property (nonatomic, strong) UILabel *showCommentLabel;

@end

@implementation LayerAffineTransformViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.peopleImage = [[UIImageView alloc] initWithFrame:CGRectMake(30, 50, 10, 10)];
   // self.peopleImage.image = [UIImage imageNamed:@"图片一"];
    self.peopleImage.backgroundColor = [UIColor redColor];
    [self.view addSubview:_peopleImage];
    
    // 添加仿射矩阵
    UIButton *addAffineButton = [UIButton buttonWithType:UIButtonTypeCustom];
    addAffineButton.frame = CGRectMake(50, 500, 150, 30);
    [addAffineButton setTitle:@"添加仿射矩阵" forState:UIControlStateNormal];
    [addAffineButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [addAffineButton addTarget:self action:@selector(addAffine:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:addAffineButton];
    
    // 恢复原状
    UIButton *deleteAffineButton = [UIButton buttonWithType:UIButtonTypeCustom];
    deleteAffineButton.frame = CGRectMake(220, 500, 100, 30);
    [deleteAffineButton setTitle:@"恢复原状" forState:UIControlStateNormal];
    [deleteAffineButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [deleteAffineButton addTarget:self action:@selector(deleteAffine:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:deleteAffineButton];

    
}

- (void)addAffine:(UIButton *)button
{
    // 展示平移200位点的正常显示坐标
    self.showCommenPointView = [[UIView alloc] initWithFrame:CGRectMake(230, 50, 10, 10)];
    self.showCommenPointView.backgroundColor = [UIColor redColor];
    [self.view addSubview:_showCommenPointView];
    self.showCommentLabel = [[UILabel alloc] initWithFrame:CGRectMake(250, 50, 100, 30)];
    self.showCommentLabel.text = @"正常点";
    self.showCommentLabel.textColor = [UIColor greenColor];
    [self.view addSubview:_showCommentLabel];
    
    // 仿射组合方法一 CGAffineTransformTranslate会保留原先transform的行为
//    CGAffineTransform affineTransform = CGAffineTransformIdentity; // 单位矩阵
//    affineTransform = CGAffineTransformScale(affineTransform, 0.5, 0.5); // 缩小2倍
//   affineTransform = CGAffineTransformRotate(affineTransform, M_PI / 180.0 * 45); // 顺时针旋转30度
//    affineTransform = CGAffineTransformTranslate(affineTransform, 200, 0); // 向右移200位点
//    self.peopleImage.layer.affineTransform = affineTransform;
    /**
     *  补充说明：这个变换你会发现其实图片并没有向右下角移动200位点，原因在于我们将scale设置为0.5的原大小，这个时候系统会将所有的位置移动都乘以0.5，因此实际上只移动了100位点
     */
    
    // 仿射组合方法二 CGAffineTransformConcat这个方法是将两个仿射矩阵组合，但是不会沿用第一个矩阵的原有行为
    CGAffineTransform transformOne = CGAffineTransformMakeScale(0.5, 0.5);
    CGAffineTransform transformTwo = CGAffineTransformMakeRotation(M_PI / 180.0 * 45);
    CGAffineTransform transformThree = CGAffineTransformMakeTranslation(150, 0);
    CGAffineTransform combineTransformOne = CGAffineTransformConcat(transformOne, transformTwo);
    CGAffineTransform combineTranformTwo = CGAffineTransformConcat(combineTransformOne, transformThree); // 如果这样的话你会发现图形缩小并旋转45度，并且向右平移150单位(不是75的原因是因为采用CGAffineTransformConcat不保留原先的行为)，而不是向右下角.
//    CGAffineTransform combineTranformTwo = CGAffineTransformTranslate(combineTransformOne, 150, 0); // 这样的话就是向右下角平移75个单位了，原因是因为scale=0.5。
    self.peopleImage.layer.affineTransform = combineTranformTwo;
    
}

- (void)deleteAffine:(UIButton *)button
{
    self.peopleImage.layer.affineTransform = CGAffineTransformIdentity;
    [self.showCommenPointView removeFromSuperview];
    [self.showCommentLabel removeFromSuperview];
}



@end
